package com.comdo.newup_loan.module.repayment_action;

import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.model.ToXuDaiApply;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/11/7.
 */

class XudaiPersenter extends XudaiContact.Presenter {
    @Override
    public void getData(int repayPlanId, int userId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("repayPlanId", repayPlanId);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().toGoOnLoan(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<ToXuDaiApply>>() {
                    @Override
                    public void accept(Result<ToXuDaiApply> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.getData(Result.getResult());
                        } else {
                            mView.getFail(Result.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }

    @Override
    public void goOnLoan(int flowId, int userId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("flowId", flowId);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().GoOnLoan(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                    @Override
                    public void accept(Result<String> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.goOnLoan(Result.getResult());
                        } else {
                            mView.getFail(Result.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }
}
